package controllers

import (
	"github.com/astaxie/beego"
	_ "github.com/go-sql-driver/mysql"
	"github.com/astaxie/beego/orm"
	"fmt"
)

type TestModelControllers struct {
	beego.Controller
}

// model Struct  例子：UserInfo name操作的表其实 user_info
type User struct {
	Id int
	Name string
	Age string
}

func (c *TestModelControllers) Get() {
	orm.Debug = true
	// set databases
	orm.RegisterDataBase("default", "mysql", "root:hjp7468668578@tcp(127.0.0.1:3306)/gomysql?charset=utf8", 30)
	// register model
	orm.RegisterModel(new(User))

	o := orm.NewOrm()

	// 插入数据
	// user := User{Name: "斗破苍穹", Age: "19"}
	// id, err := o.Insert(&user)

	// 更新数据
	// user := User{Name: "斗破苍穹", Age: "19"}
	// user.Id = 4
	// user.Name = "斗罗大陆"
	// id, err := o.Update(&user)

	// 读数据
	// user := User{Name: "斗破苍穹", Age: "19"}
	// user.Id = 4
	// o.Read(&user)

	// 原生读取
	var maps []orm.Params
	o.Raw("select * from user").Values(&maps)
	for _, v := range maps {
		fmt.Println(v)
	}
	c.Ctx.WriteString("hello")
	
}
